<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/murmur/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/murmur/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/murmur/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/murmur/images/logo.svg" color="#222">

<link rel="stylesheet" href="/murmur/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/murmur/","scheme":"Gemini","version":"8.0.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="第1章：作用域是什么 我们通过var声明变量时，是否考虑过这些问题： 这些变量都存储在哪里？ 程序用到它们时，又是怎么找到它们的？   而答案就是：不仅仅是JavaScript，任何编程语言都会设计一套良好的规则来存取变量，而这套规则就叫做 作用域 。  1.1 编译原理 虽然和静态语言(比如Java)不同，JavaScript是“解释性”的动态语言。 但实际上，JavaScript代码在运行之前">
<meta property="og:type" content="article">
<meta property="og:title" content="《你不知道的JavaScript(上)》S01E01">
<meta property="og:url" content="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS01E01/index.html">
<meta property="og:site_name" content="汇话板">
<meta property="og:description" content="第1章：作用域是什么 我们通过var声明变量时，是否考虑过这些问题： 这些变量都存储在哪里？ 程序用到它们时，又是怎么找到它们的？   而答案就是：不仅仅是JavaScript，任何编程语言都会设计一套良好的规则来存取变量，而这套规则就叫做 作用域 。  1.1 编译原理 虽然和静态语言(比如Java)不同，JavaScript是“解释性”的动态语言。 但实际上，JavaScript代码在运行之前">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-11-29T07:09:24.000Z">
<meta property="article:modified_time" content="2020-12-01T15:07:30.433Z">
<meta property="article:author" content="William Leung">
<meta property="article:tag" content="你不知道的JavaScript">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS01E01/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>《你不知道的JavaScript(上)》S01E01 | 汇话板</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?a49081c570a782a242cdf6e19e1a584d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/murmur/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">汇话板</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">设定目标，放慢步调，用温和的力量自我改革</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/murmur/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/murmur/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AC%AC1%E7%AB%A0%EF%BC%9A%E4%BD%9C%E7%94%A8%E5%9F%9F%E6%98%AF%E4%BB%80%E4%B9%88"><span class="nav-number">1.</span> <span class="nav-text">第1章：作用域是什么</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 编译原理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%AC%AC1%E6%AD%A5%EF%BC%9A%E5%88%86%E8%AF%8D-%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90%EF%BC%88Tokenizing-Lexing%EF%BC%89"><span class="nav-number">1.1.0.0.1.</span> <span class="nav-text">第1步：分词&#x2F;词法分析（Tokenizing&#x2F;Lexing）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%AC%AC2%E6%AD%A5%EF%BC%9A%E8%A7%A3%E6%9E%90-%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90%EF%BC%88Parsing%EF%BC%89"><span class="nav-number">1.1.0.0.2.</span> <span class="nav-text">第2步：解析&#x2F;语法分析（Parsing）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%AC%AC3%E6%AD%A5%EF%BC%9A%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90"><span class="nav-number">1.1.0.0.3.</span> <span class="nav-text">第3步：代码生成</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E7%90%86%E8%A7%A3%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 理解作用域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E4%BD%9C%E7%94%A8%E5%9F%9F%E5%B5%8C%E5%A5%97"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 作用域嵌套</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-%E5%BC%82%E5%B8%B8"><span class="nav-number">1.4.</span> <span class="nav-text">1.4 异常</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-5-%E5%B0%8F%E7%BB%93"><span class="nav-number">1.5.</span> <span class="nav-text">1.5 小结</span></a></li></ol></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">William Leung</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/murmur/archives">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/murmur/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/murmur/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS01E01/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/murmur/images/avatar.gif">
      <meta itemprop="name" content="William Leung">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="汇话板">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          《你不知道的JavaScript(上)》S01E01
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-11-29 15:09:24" itemprop="dateCreated datePublished" datetime="2020-11-29T15:09:24+08:00">2020-11-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2020-12-01 23:07:30" itemprop="dateModified" datetime="2020-12-01T23:07:30+08:00">2020-12-01</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/murmur/categories/%E4%B8%80%E5%91%A8%E4%B8%80%E7%AB%A0%E5%89%8D%E7%AB%AF%E4%B9%A6/" itemprop="url" rel="index"><span itemprop="name">一周一章前端书</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>2 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="第1章：作用域是什么"><a href="#第1章：作用域是什么" class="headerlink" title="第1章：作用域是什么"></a>第1章：作用域是什么</h1><ul>
<li>我们通过<code>var</code>声明变量时，是否考虑过这些问题：<ul>
<li>这些变量都存储在哪里？</li>
<li>程序用到它们时，又是怎么找到它们的？</li>
</ul>
</li>
<li>而答案就是：不仅仅是JavaScript，任何编程语言都会设计一套良好的规则来存取变量，而这套规则就叫做 <strong>作用域</strong> 。</li>
</ul>
<h2 id="1-1-编译原理"><a href="#1-1-编译原理" class="headerlink" title="1.1 编译原理"></a>1.1 编译原理</h2><ul>
<li>虽然和静态语言(比如Java)不同，JavaScript是“解释性”的动态语言。</li>
<li>但实际上，JavaScript代码在运行之前也是需要编译的，并且JavaScript引擎编译的步骤，和传统的编译语言非常相似，大致有以下三大步骤：<h5 id="第1步：分词-词法分析（Tokenizing-Lexing）"><a href="#第1步：分词-词法分析（Tokenizing-Lexing）" class="headerlink" title="第1步：分词/词法分析（Tokenizing/Lexing）"></a>第1步：分词/词法分析（Tokenizing/Lexing）</h5></li>
<li>任何<code>.js</code>文件在解析前，对于JS引擎而言都是一大段文本，不能直接运行。所以当务之急，就是将文本字符串“大卸八块”般的进行分解。</li>
<li>词法分析就是 <strong>将文本内容分解成有意义的词法字符串</strong> （token） 。</li>
<li>比如<code>var a = 2;</code>最终会分解成词法字符串数组，得到 [<code>var</code>、<code>a</code>、<code>=</code>、<code>2</code>、<code>;</code>]，而多余的空格则是无意义的。</li>
</ul>
<h5 id="第2步：解析-语法分析（Parsing）"><a href="#第2步：解析-语法分析（Parsing）" class="headerlink" title="第2步：解析/语法分析（Parsing）"></a>第2步：解析/语法分析（Parsing）</h5><ul>
<li>语法分析则是 <strong>将词法字符串数组转换成 “抽象语法树”</strong> （Abstract Syntax Tree，<strong>AST</strong> ）</li>
<li>比如代码<code>var a = 2;</code>会生成以下具有层次结构的对象<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*变量声明的对象*/</span></span><br><span class="line">VariableDeclaration : &#123;</span><br><span class="line">  <span class="comment">/*变量名为 a*/</span></span><br><span class="line">  Identifier : a,</span><br><span class="line">  <span class="comment">/*变量赋值表达式*/</span></span><br><span class="line">  AssignmentExpression : &#123;</span><br><span class="line">      <span class="comment">/*数值类型为 2*/</span></span><br><span class="line">      NumericLiteral : <span class="number">2</span> </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h5 id="第3步：代码生成"><a href="#第3步：代码生成" class="headerlink" title="第3步：代码生成"></a>第3步：代码生成</h5><ul>
<li>最后一步就是生成代码， <strong>将AST转换为可执行的机器指令</strong> 。</li>
<li>比如代码<code>var a = 2;</code>会创建一个变量<code>a</code>，并为其分配内存，然后将值<code>2</code>存进这个变量。</li>
</ul>
<h2 id="1-2-理解作用域"><a href="#1-2-理解作用域" class="headerlink" title="1.2 理解作用域"></a>1.2 理解作用域</h2><blockquote>
<p>原书将引擎、编译器以及作用域模拟成三个演员，用来说明在执行一段代码时，三者分别负责的工作。但我稍微做一些改动，将作用域比喻成一个记录清单。</p>
</blockquote>
<ul>
<li>执行JS代码依赖三个东西：<ul>
<li><strong><code>引擎</code></strong> ：负责JS代码的编译和执行</li>
<li><strong><code>编译器</code></strong> ：在引擎工作前，负责语法分析和代码生成</li>
<li><strong><code>作用域</code></strong> ：一个具有严格的规则，专门负责收集并维护所有变量的清单列表，通过它来存取变量</li>
</ul>
</li>
<li>阅读代码 <code>var a = 2;</code> 其实访问了两次作用域，一个是 <strong>在编译器编译时检查变量声明</strong> ，一个是 <strong>引擎运行时检查使用</strong> ：<ul>
<li>如上面所说的，第1步编译器会进行词法分析，第2步将词法单元解析成一个树结构的对象；</li>
<li>在第3步生成代码时，编译器会去查找作用域，检查 是否存在同名的变量，如果没有则声明一个新的变量并赋值 ；</li>
<li>最后引擎运行代码时，会再次通过作用域 检查 是否存在同名的变量，如果有则直接 使用，没有则继续向上查找</li>
</ul>
</li>
<li>引擎执行代码到作用域查找变量，分为两种类型：<strong>RHS查询</strong> 和 <strong>LHS查询</strong> ：<ul>
<li>“L（left）”和“R（right）”分别代表变量处于表达式的左边还是右边；</li>
<li>RHS查询就是查找变量，可理解成retrieve his source value（找到它源值）。比如<code>console.log(a)</code>就是RHS查询，找到变量<code>a</code>的值传递给<code>console.log()</code>；</li>
<li>LHS查询则是查找变量的容器对其进行赋值。比如<code>var a = 2;</code>就是LHS查询，找到变量<code>a</code>并为它赋值<code> = 2</code>；</li>
</ul>
</li>
<li>我们尝试用RHS查询和LHS查询的思维来阅读JS代码：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">a</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a);</span><br><span class="line">&#125;</span><br><span class="line">foo(<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
我们都知道<code>function</code>声明函数的方式等同于，声明一个变量并为其赋值一个执行方法体：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = <span class="function"><span class="keyword">function</span>(<span class="params">a</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a);</span><br><span class="line">&#125;</span><br><span class="line">foo(<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li><code>var foo = function()</code>这是一个LHS查询：声明<code>foo</code>变量并为其赋值一个方法；</li>
<li><code>foo(2)</code>属于RHS查询：找到<code>foo</code>变量的值并执行它</li>
<li>进到<code>foo</code>方法体中，实际上这里隐藏了一句代码<code>a = 2;</code>将传递的值赋值给形参</li>
<li><code>console.log(a)</code>是RHS查询：找到<code>a</code>的值，传递给<code>console.log(...)</code></li>
<li>值得一提的是，<code>console.log()</code>本身也属于RHS查询，会去找寻<code>log()</code>方法的引用并执行它</li>
</ul>
</li>
</ul>
<h2 id="1-3-作用域嵌套"><a href="#1-3-作用域嵌套" class="headerlink" title="1.3 作用域嵌套"></a>1.3 作用域嵌套</h2><ul>
<li>不管是RHS查询还是LHS查询都从当前作用域开始，如果当前作用域无法找到变量时，引擎会转移到外层作用域中继续查找，直至转移到最顶层的作用域，也就是全局作用域。</li>
<li>举例：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">a</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a + b);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> b = <span class="number">2</span>;</span><br><span class="line">foo(<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
在<code>foo</code>方法体中，变量<code>b</code>在<code>foo</code>的作用域中找不到，将会到外层的全局作用域查找，最后输出4</li>
</ul>
<h2 id="1-4-异常"><a href="#1-4-异常" class="headerlink" title="1.4 异常"></a>1.4 异常</h2><ul>
<li>之所以 区分RHS和LHS，是因为当查找到未声明的变量时，这两种查询的行为是不一样的：<ul>
<li>如前文提到的，LHS查询失败时会在全局作用域创建一个同名的变量；</li>
<li>而RHS查询失败时，则会抛出 <strong>ReferenceError异常</strong> ；另一种情况是，查找到了变量，但是尝试对这个变量的值做不合理的操作（比如对一个非函数的变量进行调用），则抛出TypeError异常</li>
<li>总而言之，RererenceError异常是作用域判别失败相关的， <strong>TypeError异常</strong> 则代表作用域判别成功了，但对结果的操作是非法或不合理的</li>
</ul>
</li>
</ul>
<h2 id="1-5-小结"><a href="#1-5-小结" class="headerlink" title="1.5 小结"></a>1.5 小结</h2><ul>
<li><strong>作用域是一套存取变量的规则</strong> ；</li>
<li>在代码执行前，会先由编译器进行编译，JavaScript引擎在执行代码时会进行LHS查询和RHS查询：<ul>
<li><strong>LHS查询是对变量进行赋值</strong> ，其中<code>=</code>操作符或者调用函数时传参的操作，都会导致相关作用域的赋值操作；</li>
<li><strong>RHS查询是对变量的值进行查找</strong> ；</li>
</ul>
</li>
<li>LHS和RHS查询都会从当前执行作用域开始，<strong>如果当前作用域找不到，就会往上级作用域继续查找，每次上升一级作用域，直至到顶级的全局作用域</strong></li>
<li><strong>不成功的RHS查询会抛出Reference异常，而不成功的LHS查询会自动式地创建一个全局变量</strong></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>William Leung
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS01E01/" title="《你不知道的JavaScript(上)》S01E01">https://gitee.com/lianghuilin/murmur.git/reading_notes/《你不知道的JavaScript-上-》S01E01/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/murmur/tags/%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript/" rel="tag"># 你不知道的JavaScript</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
            </div>
            <div class="post-nav-item">
                <a href="/murmur/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS01E02/" rel="next" title="《你不知道的JavaScript(上)》S01E02">
                  《你不知道的JavaScript(上)》S01E02 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">William Leung</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">142k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:09</span>
  </span>
</div>
<div class="busuanzi-count">
    <span id="busuanzi_container_site_uv">
      本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/murmur/js/utils.js"></script><script src="/murmur/js/motion.js"></script><script src="/murmur/js/next-boot.js"></script>

  
















  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  

</body>
</html>
